﻿using CSRedis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseData.Infrastructure
{
    public class RedisService<T>
    {
        private readonly CSRedisClient csredis;

        public RedisService(CSRedisClient csredis)
        {
            this.csredis = csredis;
        }
        /// <summary>
        /// 异步的缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="func">委托的无参有返的方法</param>
        /// <param name="expireTime">过期时间</param>
        /// <returns></returns>
        public async Task<List<T>> GetListAsync(string key,Func<Task<List<T>>> func,int expireTime)
        {
            var client = await csredis.GetAsync<List<T>>(key);
            if (client != null)
            {
                return client;
            }
            else
            {
                var res = await func();
                var result = await csredis.SetAsync(key, res, expireTime);
                return res;
            }
        }

    }
}
